<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .swiper-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }
        .swiper-container {
            background-color: #fff;
        }
        .layui-form-select {
            width: 500px;
        }
    </style>
</head>

<body>
    <div class="swiper-container">
        <table lay-filter="swiperTable" class="layui-hide" id="swiperTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery'], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            $ = layui.jquery;

        table.render({
            elem: '#swiperTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/indexProducts/getCarousel'
            , page: false
            , headers: {
                Authorization: localStorage.token
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                { field: 'id', width: 80, title: 'ID', sort: true }
                , { field: 'title', title: '名称' }
                , { field: 'remarks', title: '描述' }
                , { field: 'urlLink', title: '轮播图类型', templet:function(res) {
                    return !res.goodsType ? "无跳转" : res.goodsType == 2 ? "分销商品" : res.goodsType == 1 ? "普通商品" : res.goodsType == 3?"会员商品":"-";
                } }
                , { field: 'sort', title: '图片排序' }
                , {
                    field: 'imgUrl', title: '图片', templet: function (res) {
                        return '<img style="width:100%; height:100%; object-fit: contain; max-width:initial;" src="' + res.imgUrl + '" />'
                    }
                }
                , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
            ]]
        });

        //工具栏事件
        table.on('toolbar(swiperTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    //页面层
                    showLayer();
                    $("#imgUrl").val("");
                    $("#swiperId").val("");
                    break;
            };
        });

        // 编辑和删除
        table.on("tool(swiperTable)", function(data) {
            switch(data.event) {
                case "del":
                layer.confirm('确定删除？', function(index){
                    ajax({
                        path: "/indexProducts/delCarousel?id=" + data.data.id,
                        type: 'GET',
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("删除成功");
                            table.reload("swiperTable");
                        }
                    }, $)
                });
                    break;
                case "edit":
                    showLayer("edit", data.data);
                    $('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', data.data.imgUrl);
                    break;
            }
        })


        function showLayer(type, da) {
            layer.open({
                type: 1,
                title: "添加/编辑轮播图",
                area: ['650px', '740px'], //宽高
                content: `
                            <form class="layui-form lay-form" lay-filter="swiperForm">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="title" lay-verify="required" lay-reqtext="用户名是必填项，岂能为空？" placeholder="请输入名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">描述</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="remarks" placeholder="请输入描述" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <!-- <div class="layui-form-item">
                                    <label class="layui-form-label">跳转连接</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="urlLink" lay-verify="required" lay-reqtext="跳转连接是必填项，岂能为空？" placeholder="请输入跳转连接" autocomplete="off" class="layui-input">
                                    </div>
                                </div> -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">图片排序</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="sort" lay-verify="required|number" lay-reqtext="图片排序必须是数字且不能为空" placeholder="请输入图片排序" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">轮播图类型</label>
                                    <div class="layui-input-block">
                                        <input type="radio" lay-filter="radio" name="goodsType" value="0" title="无跳转" checked="">
                                        <input type="radio" lay-filter="radio" name="goodsType" value="1" title="普通商品">
                                        <input type="radio" lay-filter="radio" name="goodsType" value="3" title="会员商品">
                                        <input type="radio" lay-filter="radio" name="goodsType" value="2" title="分销商品">
                                    </div>
                                </div>

                                <div class="layui-form-item" id="glsp" style="display:none">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">关联商品</label>
                                        <div class="layui-input-inline">
                                            <select name="goodsId" id="goodsList" lay-search="" style="width: 300px;">
                                                <option value="">请选择关联商品</option>
                                                
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item" id="glsp1" style="display:none">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">关联会员商品</label>
                                        <div class="layui-input-inline">
                                            <select name="goodsVipId" id="goodsList1" lay-search="" style="width: 300px;">
                                                <option value="">请选择关联会员商品</option>

                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <input type="hidden" name="imgUrl" id="imgUrl"/>
                                <input type="hidden" name="id" id="swiperId""/>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">图片上传</label>
                                    <div class="layui-upload-drag" id="uploadimg">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="uploadDemoView">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                
                                
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="swiperSubmit">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    </div>
                                </div>
                            </form>
                        `,
                        success() {
                            ajax({
                                path: "/goods/getMallGoodsList?special=0&status=1&length=9999999",
                                type:"GET",
                                success: function (res) {
                                    $("#goodsList").html('<option value="">请选择或搜索关联商品</option>')
                                    res.data.forEach(el => {
                                        if(el.vip == 0){
                                            $("#goodsList").append('<option value="'+ el.skuId+'">'+ el.goodsName +'('+ el.skuTitleType + '：'+ el.skuTitle +')</option>')
                                        }
                                    })

                                    $("#goodsList1").html('<option value="">请选择或搜索关联会员商品</option>')
                                    res.data.forEach(el => {
                                        if(el.vip == 1){
                                            $("#goodsList1").append('<option value="'+ el.skuId+'">'+ el.goodsName +'('+ el.skuTitleType + '：'+ el.skuTitle +')</option>')
                                        }
                                    })

                                    if (type) {
                                        if (da.goodsType == 1) {
                                            $("#glsp").show();
                                        }
                                        if (da.goodsType == 3) {
                                            $("#glsp1").show();
                                        }
                                        
                                        form.val('swiperForm', da);
                                    }

                                    form.render();
                                }
                            }, $)

                            form.render();


                            form.on('radio(radio)', function(data){
                                if (data.value == 1) {
                                    $("#glsp").show();
                                } else {
                                    $("#glsp").hide();
                                }
                                if (data.value == 3) {
                                    $("#glsp1").show();
                                } else {
                                    $("#glsp1").hide();
                                }
                            });
                        }
            });

            uploads(upload, '#uploadimg');
            // form 提交
            form.on("submit(swiperSubmit)", function (el) {
                console.log(el.field)
                // 编辑
                if(el.field.goodsType == 3){
                    el.field.goodsId =el.field.goodsVipId;
                }
                if (el.field.id) {
                    ajax({
                        path: "/indexProducts/editCarousel",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("编辑成功");
                            table.reload("swiperTable");
                            form.render();
                        }
                    }, $)
                } 
                // 添加
                else {
                    ajax({
                        path: "/indexProducts/addCarousel",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("添加成功");
                            table.reload("swiperTable");
                            form.render();
                        }
                    }, $)
                }
                return false;
            })
        }
    });
</script>

</html>